/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-08-28
*/
package com.rzico.market.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.market.entity.InvoiceItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.market.mapper.InvoiceItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * <pre>
 * 开票商品支付信息业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class InvoiceItemService extends BaseServiceImpl<InvoiceItem, String> {

    @Autowired
    private InvoiceItemMapper invoiceItemMapper;

    @Override
    public BaseMapper<InvoiceItem, String> getMapper() {
        return invoiceItemMapper;
    }

    /**
     * 保存开票商品支付信息
     * @param map
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public InvoiceItem saveInvoiceItem(Map<String, Object> map) {
        InvoiceItem invoiceItem = new InvoiceItem();
        invoiceItem.setCreateDate(new Date());
        invoiceItem.setDeleted(false);
        invoiceItem.setState(false);
        invoiceItem.setInvoiceId((Long) map.get("invoiceId"));
        invoiceItem.setMethod((Integer) map.get("method"));
        //值为null则用不同的方式处理
        String pluginId = map.getOrDefault("pluginId", "").toString();
        String pluginName = map.getOrDefault("pluginName", "").toString();
        invoiceItem.setPaymentPluginId(pluginId);
        invoiceItem.setPaymentPluginName(pluginName);
        if (map.containsKey("companyId")) {
            invoiceItem.setCompanyId((Long) map.get("companyId"));
        } else {
            invoiceItem.setCompanyId(0L);
        }
        if (map.containsKey("cardId")) {
            invoiceItem.setCardId((Long) map.get("cardId"));
        } else {
            invoiceItem.setCardId(0L);
        }
        invoiceItem.setCardCode(map.getOrDefault("cardCode", "").toString());
        invoiceItem.setCardMemo(map.getOrDefault("cardMemo", "").toString());
        invoiceItem.setAmount(new BigDecimal(map.get("amount").toString()));
        invoiceItem.setInvoiceAmount(new BigDecimal(0));
        invoiceItem.setOutInvoiceQuantity(new BigDecimal(0));
        invoiceItem.setEnterpriseId((Long) map.get("enterpriseId"));
        invoiceItemMapper.insertUseGeneratedKeys(invoiceItem);
        InvoiceItem result = super.findById(invoiceItem.getId());
        return result;
    }

}
